package copyengine.ui.infoTree.buildTree.builder
{
    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.infoTree.meta.CESSpriteMeta;
    import copyengine.ui.infoTree.buildTree.InfoTreeBuildUtils;
    import copyengine.utils.CEStringUtils;

    import flash.utils.Dictionary;

    import starling.display.DisplayObject;
    import starling.display.Sprite;

    public class InfoTreeBuilderSprite extends InfoTreeBuilderBasic
    {
        public function InfoTreeBuilderSprite()
        {
            super();
        }

        override public function isCanHandleMeta(_meta:*):Boolean {return _meta is CESSpriteMeta;}

        override public function getBuildResultMc(_meta:*, _quickFindDic:Dictionary, _quickFindKeyPR:String, _isRoot:Boolean):DisplayObject
        {
            var meta:CESSpriteMeta = _meta as CESSpriteMeta;
            InfoTreeBuildUtils.sortContainerSubChild(meta.childMetaArray);

            var childQuickPR:String;
            var childQuickFindDic:Dictionary;
            if (_isRoot) {
                childQuickPR = "root";
                childQuickFindDic = _quickFindDic;
            } else {
                childQuickPR = _quickFindKeyPR + "[" + meta.name + "]";
                childQuickFindDic = CEStringUtils.beginsWith(meta.name, "instance") == true ? null : _quickFindDic;
            }

            var sp:Sprite = new Sprite();
            var totalChildNum:int = meta.childMetaArray.length;
            for (var i:int = 0; i < totalChildNum; i++) {
                var childMeta:CESDisplayObjectMeta = meta.childMetaArray[i];
                var child:DisplayObject = mBuildHelper.getBuilderByMeta(childMeta).getBuildResultMc(childMeta, childQuickFindDic, childQuickPR, false);
                sp.addChild(child);
            }

            InfoTreeBuildUtils.setBasicInfoByMeta(meta, sp);
            InfoTreeBuildUtils.addToQuickFindDic(sp, _quickFindDic, _quickFindKeyPR, _isRoot);
            return sp;
        }

    }
}
